package com.zc.mychart.chart;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.zc.mychart.R;
import com.zc.mychart.model.EntryData;

/**
 * Created by zhangchi on 2018/3/7.
 */

public class MarkerViewX extends MarkerView {
    private TextView tv_content;
    private MPPointF mOffset = new MPPointF();

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MarkerViewX(Context context, int layoutResource) {
        super(context, layoutResource);
        tv_content = (TextView) findViewById(R.id.tv_content);
    }

    public void setMarkerBg(int resId, int textColor) {
        tv_content.setBackgroundResource(resId);
        tv_content.setTextColor(textColor);
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        if(null != e && null != e.getData()){
            if(e.getData() instanceof EntryData){
                tv_content.setText(((EntryData)e.getData()).time);
            }else{
                tv_content.setText(e.getData().toString());
            }
        }
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        Chart chart = getChartView();
        if (chart == null)
            return mOffset;
        float width = getWidth();
        float height = getHeight();
        mOffset.x = -getWidth() / 2.0f;
        mOffset.y = chart.getHeight() - posY - height - chart.getXAxis().getYOffset();
        if (posX + mOffset.x < 0) {
            mOffset.x = -posX;
        } else if (chart != null && posX + width + mOffset.x > chart.getWidth()) {
            mOffset.x = chart.getWidth() - posX - width;
        }
        return mOffset;
    }
}
